<template>
  <div class="question-container">
    <el-card class="box-card">
      <el-row type="flex" justify="end">
        <!-- // 新建试题按钮 -->
        <el-button
          type="success"
          icon="el-icon-edit"
          size="small"
          style="margin: 10px"
          @click="$router.push('/questions/new')"
        >
          新建试题
        </el-button>
      </el-row>
      <!-- //顶部筛选搜索框 -->
      <el-form
        ref="form"
        label-width="80px"
        size="small"
        :model="form"
      >
        <el-row>
          <!-- // 第一行 -->
          <!--  学科 -->
          <el-col :span="6">
            <el-form-item label="学科">
              <el-select
                v-model="form.subjectID"
                placeholder="请选择"
                style="width: 100%"
                @change="changeSubject"
              >
                <el-option
                  v-for="item in subjectList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 二级目录 -->
          <el-col :span="6">
            <el-form-item label="二级目录">
              <el-select
                v-model="form.catalogID"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in directoryList"
                  :key="item.id"
                  :label="item.directoryName"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 标签 -->
          <el-col :span="6">
            <el-form-item label="标签">
              <el-select
                v-model="form.tags"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in tagsList"
                  :key="item.id"
                  :label="item.tagName"
                  :value="item.tagName"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 关键字 -->
          <el-col :span="6">
            <el-form-item label="关键字">
              <el-input v-model="form.keyword" style="width: 100%" />
            </el-form-item>
          </el-col>
          <!-- // 第二行 -->
          <!-- 试题类型 -->
          <el-col :span="6">
            <el-form-item label="试题类型">
              <el-select
                v-model="form.questionType"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in questionTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 难度 -->
          <el-col :span="6">
            <el-form-item label="难度">
              <el-select
                v-model="form.difficulty"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in difficultyList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 方向 -->
          <el-col :span="6">
            <el-form-item label="方向">
              <el-select
                v-model="form.direction"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in directionList"
                  :key="item"
                  :label="item"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 录入人 -->
          <el-col :span="6">
            <el-form-item label="录入人">
              <el-select
                v-model="form.creatorID"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in creatorList"
                  :key="item.id"
                  :label="item.username"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <!-- // 第三行 -->
          <!-- 题目备注 -->
          <el-col :span="6">
            <el-form-item label="题目备注">
              <el-input v-model="form.remarks" style="width: 100%" />
            </el-form-item>
          </el-col>
          <!-- 企业简称 -->
          <el-col :span="6">
            <el-form-item label="企业简称">
              <el-input v-model="form.shortName" style="width: 100%" />
            </el-form-item>
          </el-col>
          <!-- 城市 -->
          <el-col :span="6">
            <el-form-item label="城市">
              <el-select
                v-model="form.province"
                placeholder="请选择"
                style="width: 50%"
              >
                <el-option
                  v-for="item in provinceList"
                  :key="item"
                  :label="item"
                  :value="item"
                />
              </el-select>

              <el-select
                v-model="form.city"
                style="width: 50%"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in getCityList"
                  :key="item"
                  :label="item"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item style="text-align:right">
              <el-button @click="clear">清除</el-button>
              <el-button type="primary" @click="getList()">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <!-- // tabs标签页 -->
      <el-tabs v-model="form.chkState" type="card" @tab-click="getList()">
        <el-tab-pane label="全部" name="all" />
        <el-tab-pane label="待审核" name="0" />
        <el-tab-pane label="已审核" name="1" />
        <el-tab-pane label="已拒绝" name="2" />
      </el-tabs>

      <!-- // 数量统计区域 -->
      <el-alert
        type="info"
        show-icon
        style="margin-bottom: 10px"
      >
        <template #title>
          <span>数据一共{{ total }}条</span>
        </template>
      </el-alert>
      <!-- // 表格区域 -->
      <el-table
        style="width: 100%"
        :data="list"
      >
        <el-table-column
          prop="number"
          label="试题编号"
          width="220"
          align="center"
        />
        <el-table-column
          prop="subject"
          label="学科"
          width="100"
          align="center"
        />
        <el-table-column
          prop="catalog"
          label="目录"
          width="100"
          align="center"
        />
        <el-table-column
          prop="name"
          label="题型"
        >
          <template v-slot="{row}">
            {{ questionTypeList.find(item => item.value === +row.questionType).label }}
          </template>
        </el-table-column>
        <el-table-column
          label="题干"
          width="200"
          align="center"
        >
          <template v-slot="{row}">
            <div v-html="row.question" />
          </template>
        </el-table-column>
        <el-table-column
          label="录入时间"
          align="center"
          width="180"
        >
          <template v-slot="{row}">
            {{ row.addDate | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column
          label="难度"
          width="100"
          align="center"
        >
          <template v-slot="{row}">
            {{ difficultyList.find(item => item.value === +row.difficulty).label }}
          </template>
        </el-table-column>
        <el-table-column
          prop="creator"
          label="录入人"
          width="100"
          align="center"
        />
        <el-table-column
          label="审核状态"
          width="100"
          align="center"
        >
          <template v-slot="{row}">
            {{ row.chkState | stateChk }}
          </template>
        </el-table-column>
        <el-table-column
          prop="chkRemarks"
          label="审核意见"
          align="center"
          width="200"
        />
        <el-table-column
          prop="chkUser"
          label="审核人"
          width="100"
          align="center"
        />
        <el-table-column
          label="发布状态"
          width="100"
          align="center"
        >
          <template v-slot="{row}">
            {{ row | statePublish }}
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="操作"
          fixed="right"
          align="center"
          width="250"
        >
          <template v-slot="{row}">
            <el-button type="text" @click="showPreview(row)">预览</el-button>
            <el-button
              type="text"
              :disabled="row.chkState === 1"
              @click="changeChkState(row)"
            >审核</el-button>
            <el-button
              type="text"
              :disabled="row.publishState === 1"
              @click="$router.push(`/questions/new?id=${row.id}`)"
            >修改</el-button>
            <el-button type="text" @click="changePublishState(row)">{{ row.publishState === 1? '下架': '上架' }}</el-button>
            <el-button type="text" :disabled="row.publishState === 1" @click="del(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- //分页 -->
      <el-row type="flex" justify="end">
        <el-pagination
          background
          :page-sizes="[5,10,20]"
          :current-page="form.page"
          :page-size="form.pagesize"
          layout="prev, pager, next, sizes, jumper"
          :total="total"
          @size-change="changeSize"
          @current-change="changeCurrent"
        />
      </el-row>
    </el-card>
    <!-- // 题目审核组件 -->
    <QuestionCheck
      :show-review-dialog.sync="showReviewDialog"
      :questions="questions"
      @updateList="getList()"
    />
    <!-- // 预览组件 -->
    <QuestionsPreview
      :show-questions-dialog.sync="showQuestionsDialog"
      :questions="questions"
      :question-type-list="questionTypeList"
      :difficulty-list="difficultyList"
    />
  </div>
</template>

<script>
import { simple as getSubjectList } from '@/api/hmmm/subjects'
import { list as getDirectoryList } from '@/api/hmmm/directorys'
import { list as getTagsList } from '@/api/hmmm/tags'
import { questionType, difficulty, direction } from '@/api/hmmm/constants'
import { list as getUserList } from '@/api/base/users'
import { provinces, citys } from '@/api/hmmm/citys'
import { choice as getQuestionList, choicePublish, remove as delQuestion } from '@/api/hmmm/questions'
import QuestionCheck from '@/module-hmmm/components/questions-check'
import QuestionsPreview from '../components/questions-preview'
export default {
  name: 'QuestionsChoice',
  filters: {
    // 审核状态的过滤器
    stateChk(value) {
      if (value === 0) {
        return '待审核'
      } else if (value === 1) {
        return '已审核'
      } else {
        return '已拒绝'
      }
    },
    // 发布状态的过滤器
    statePublish(row) {
      if (row.publishState === 0 && row.chkState === 1) {
        return '已下架'
      } else if (row.publishState === 1 && row.chkState === 1) {
        return '已发布'
      } else {
        return '待发布'
      }
    }
  },
  components: {
    QuestionCheck,
    QuestionsPreview
  },
  data() {
    return {
      form: {
        subjectID: null, // 学科id
        catalogID: null, // 目录id
        tags: null, // 标签名字
        keyword: null, // 关键字
        questionType: null, // 试题类型
        difficulty: null, // 难度
        direction: null, // 方向
        creatorID: null, // 录入人
        remarks: null, // 题目备注
        shortName: null, // 企业简称
        province: null, // 企业所在省份
        city: null, // 企业所在城市
        chkState: 'all', // 审核状态
        page: 1, // 当前显示的页数
        pagesize: 5 // 每页显示的条数
      },
      subjectList: [], // 学科列表
      directoryList: [], // 目录列表
      tagsList: [], // 标签列表
      questionTypeList: questionType, // 试题类型列表
      difficultyList: difficulty, // 难度列表
      directionList: direction, // 方向列表
      // chkTypeList: chkType, // 审核状态列表
      // publishTypeList: publishType, // 发布状态列表
      creatorList: [], // 录入人列表
      provinceList: provinces(), // 省份列表
      list: [], // 所有精选试题列表
      // tableList: [], // table展示题库列表
      total: 0, // 总条数
      totalNumber: 0, // alert 假数据 后面删
      showReviewDialog: false, // 控制审核弹窗
      questions: {}, // 当前试题的信息
      showQuestionsDialog: false
    }
  },
  computed: {
    // 提供城市列表计算属性
    getCityList() {
      return citys(this.form.province)
    }
  },
  created() {
    this.getSubjectList()
    this.getCreatorList()
    this.getList()
  },
  methods: {
    // 获取学科列表
    async getSubjectList() {
      const { data } = await getSubjectList()
      // console.log(data)
      this.subjectList = data
    },
    // 获取目录和标签列表
    async changeSubject() {
      const subjectID = this.form.subjectID
      if (subjectID) {
        const { data } = await getDirectoryList({ subjectID })
        this.directoryList = data.items

        const { data: res } = await getTagsList(subjectID)
        this.tagsList = res.items
        // console.log(this.tagsList)
      }
    },
    // 获取录入人列表
    async getCreatorList() {
      const { data } = await getUserList()
      // console.log(data)
      this.creatorList = data.list
    },
    // 清除表单已选项
    clear() {
      this.form = {
        subjectID: null, // 学科id
        catalogID: null, // 目录id
        tags: null, // 标签名字
        keyword: null, // 关键字
        questionType: null, // 试题类型
        difficulty: null, // 难度
        direction: null, // 方向
        creatorID: null, // 录入人
        remarks: null, // 题目备注
        shortName: null, // 企业简称
        province: null, // 企业所在省份
        city: null, // 企业所在城市
        chkState: 'all', // 审核状态
        page: 1, // 当前显示的页数
        pagesize: 5 // 每页显示的条数
      }
    },
    // 获取精选题库列表
    async getList() {
      const formDate = { ...this.form }
      if (formDate.chkState === 'all') formDate.chkState = null
      const { data } = await getQuestionList(formDate)
      // console.log(data)
      this.list = data.items
      this.total = data.counts
    },
    // 根据条件获取精选题库列表
    // search() {
    //   this.form.page = 1
    //   this.getList()
    // },
    // tabs切换根据审核状态获取题库列表
    // tabChange() {
    //   // console.log(this.tabsName)
    //   // if (this.tabsName === 'Be taken') {
    //   //   this.form.chkState = 0
    //   //   this.getList()
    //   // } else if (this.tabsName === 'Reviewed') {
    //   //   this.form.chkState = 1
    //   //   this.getList()
    //   // } else if (this.tabsName === 'rejected') {
    //   //   this.form.chkState = 2
    //   //   this.getList()
    //   // } else if (this.tabsName === 'all') {
    //   //   this.getList()
    //   // }
    //   this.getList()
    // },
    // 分页按钮改变每页显示条数触发
    changeSize(value) {
      // console.log(value)
      this.form.pagesize = value
      this.getList()
    },
    // 当前页改变的时候触发
    changeCurrent(value) {
      console.log(value)
      this.form.page = value
      this.getList()
    },
    // 点击审核按钮触发
    changeChkState(row) {
      this.showReviewDialog = true
      this.questions = row
    },
    // 上下架功能
    async changePublishState(row) {
      // 思路: 当已完成审核的时候才允许操作上下架
      if (row.publishState === 1 && row.chkState === 1) {
        await this.$confirm('您确定下架这道题目吗?', '温馨提示', {
          type: 'warning'
        })
        row.publishState = 0
      } else if (row.publishState === 0 && row.chkState === 1) {
        await this.$confirm('您确定上架这道题目吗?', '温馨提示', {
          type: 'warning'
        })
        row.publishState = 1
      } else {
        return this.$message('请先完成题目的审核')
      }
      await choicePublish(row)
      this.$message.success('操作成功')
      this.getList()
    },
    // 删除 功能
    async del(row) {
      try {
        await this.$confirm('此操作将永久删除该题目', '温馨提示', {
          type: 'warning'
        })
      } catch {
        return
      }
      await delQuestion({ id: row.id })
      this.$message.success('删除成功')
      this.getList()
    },
    // 显示预览题目dialog
    showPreview(row) {
      this.showQuestionsDialog = true
      this.questions = row
    }
  }
}
</script>

<style scoped lang='scss'>
.question-container {
  padding: 0 10px;
  margin: 10px 0;
}
</style>
